package com.servlet;

import com.dao.SupplierDao;
import com.dao.impl.SupplierDaoImpl;
import com.entry.Goods;
import com.entry.Supplier;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.util.PageModel;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(urlPatterns = "/system/sup.do")
public class SupplierServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        HttpSession session = req.getSession();
        String ainto = req.getParameter("ainto");
        SupplierDao supplierDao = new SupplierDaoImpl();
        int pageSize = req.getParameter("pageSize") == null ? 5 : Integer.parseInt(req.getParameter("pageSize").toString());
        int pageNo = req.getParameter("pageNo") == null ? 1 : Integer.parseInt(req.getParameter("pageNo").toString());
        if(ainto == null) {
            PageModel<Supplier> pm = supplierDao.findAll(pageSize,pageNo);
            pm.setPageNav(req.getRequestURI());
            session.setAttribute("sup", pm);
            resp.sendRedirect("./sup.jsp");
        }else if("add".equals(ainto)) {
            JsonParser jParser = new JsonParser();
            JsonObject jt = (JsonObject) jParser.parse(req.getParameter("c"));//构造JsonObject对象
            String supname = jt.get("supname").getAsString();
            String supar = jt.get("supar").getAsString();
            if ("".equals(supname) || supname == null || "".equals(supar) || supar == null) {
                session.setAttribute("supAddMess", "请完善信息");
            } else {
                if (supplierDao.findId(supname) != null) {
                    session.setAttribute("supAddMess", "编号已经存在");
                } else {
                    Supplier supplier = new Supplier();
                    supplier.setSup_name(supname);
                    supplier.setSup_address(supar);
                    int ro = supplierDao.add(supplier);
                    if (ro != 0) {
                        session.setAttribute("supAddMess", "添加成功");
                        PageModel<Supplier> pm = supplierDao.findAll(pageSize,pageNo);
                        pm.setPageNav(req.getRequestURI());
                        session.setAttribute("sup", pm);
                    } else {
                        session.setAttribute("supAddMess", "添加失败");
                    }
                }
            }
            resp.sendRedirect("./sup.jsp");
        }else if("update1".equals(ainto)) {
            String name = req.getParameter("name");
            Supplier supplier = supplierDao.findId(name);
            session.setAttribute("supplier", supplier);
            resp.sendRedirect("./sup.jsp");
        }else if("update".equals(ainto)) {
            JsonParser jParser = new JsonParser();
            JsonObject jt = (JsonObject) jParser.parse(req.getParameter("c"));//构造JsonObject对象
            String supname = jt.get("supname").getAsString();
            String suparr = jt.get("suparr").getAsString();
            if ("".equals(supname) || supname == null || "".equals(suparr) || suparr == null) {
                session.setAttribute("supUpMess", "请完善信息");
            } else {
                Supplier supplier = new Supplier();
                supplier.setSup_name(supname);
                supplier.setSup_address(suparr);
                if (supplierDao.update(supplier) != 0) {
                    session.setAttribute("supUpMess", "修改成功");
                    PageModel<Supplier> pm = supplierDao.findAll(pageSize,pageNo);
                    pm.setPageNav(req.getRequestURI());
                    session.setAttribute("sup", pm);
                    session.setAttribute("supplier", supplier);
                } else {
                    session.setAttribute("supUpMess", "修改失败");
                }
            }
            resp.sendRedirect("./sup.jsp");
        }else if("delete".equals(ainto)){
            String name = req.getParameter("name");
            if(supplierDao.delete(name) != 0){
                session.setAttribute("supdeMess","删除成功");
            }else{
                session.setAttribute("supdeMess","删除失败");
            }
            PageModel<Supplier> pm = supplierDao.findAll(pageSize,pageNo);
            pm.setPageNav(req.getRequestURI());
            session.setAttribute("sup", pm);
            resp.sendRedirect("./sup.jsp");
        }
    }
}
